{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c3f248fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting stargazer\n",
      "  Downloading stargazer-0.0.7-py3-none-any.whl (15 kB)\n",
      "Installing collected packages: stargazer\n",
      "Successfully installed stargazer-0.0.7\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "Placebo Tests for the First Difference-in-Differences Model<br><table style=\"text-align:center\"><tr><td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td colspan=\"6\"><em>Dependent variable: Employment</em></td></tr><tr><td style=\"text-align:left\"></td><tr><td></td><td colspan=\"1\">2014 & 2015</td><td colspan=\"1\">2014 & 2016</td><td colspan=\"1\">2014 & 2017</td><td colspan=\"1\">2015 & 2016</td><td colspan=\"1\">2015 & 2017</td><td colspan=\"1\">2016 & 2017</td></tr><tr><td style=\"text-align:left\"></td><td>(1)</td><td>(2)</td><td>(3)</td><td>(4)</td><td>(5)</td><td>(6)</td></tr>\n",
       "<tr><td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr>\n",
       "\n",
       "<tr><td style=\"text-align:left\">Intercept</td><td>32574.43<sup>***</sup></td><td>32574.43<sup>***</sup></td><td>32574.43<sup>***</sup></td><td>30385.50<sup>***</sup></td><td>30385.50<sup>***</sup></td><td>27443.97<sup>***</sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(6733.29)</td><td>(6733.29)</td><td>(6733.29)</td><td>(6057.65)</td><td>(6057.65)</td><td>(5303.14)</td></tr>\n",
       "<tr><td style=\"text-align:left\">Treat</td><td>-15408.50<sup>**</sup></td><td>-15408.50<sup>**</sup></td><td>-15408.50<sup>**</sup></td><td>-14425.68<sup>**</sup></td><td>-14425.68<sup>**</sup></td><td>-12345.25<sup>**</sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(7012.79)</td><td>(7012.79)</td><td>(7012.79)</td><td>(6339.13)</td><td>(6339.13)</td><td>(5600.27)</td></tr>\n",
       "<tr><td style=\"text-align:left\">FalsePost</td><td>-2188.94<sup></sup></td><td>-5130.47<sup></sup></td><td>-5828.21<sup></sup></td><td>-2941.53<sup></sup></td><td>-3639.28<sup></sup></td><td>-697.75<sup></sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(9057.17)</td><td>(8570.91)</td><td>(8408.32)</td><td>(8050.99)</td><td>(7877.67)</td><td>(7313.40)</td></tr>\n",
       "<tr><td style=\"text-align:left\">Treat×FalsePost</td><td>982.82<sup></sup></td><td>3063.25<sup></sup></td><td>3621.47<sup></sup></td><td>2080.43<sup></sup></td><td>2638.65<sup></sup></td><td>558.22<sup></sup></td></tr>\n",
       "<tr><td style=\"text-align:left\"></td><td>(9453.24)</td><td>(8974.53)</td><td>(8823.09)</td><td>(8458.58)</td><td>(8297.72)</td><td>(7747.95)</td></tr>\n",
       "\n",
       "<td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr>\n",
       "<tr><td style=\"text-align: left\">Observations</td><td>892</td><td>892</td><td>892</td><td>892</td><td>892</td><td>892</td></tr><tr><td style=\"text-align: left\">R<sup>2</sup></td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td></tr><tr><td style=\"text-align: left\">Adjusted R<sup>2</sup></td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td><td>0.01</td></tr><tr><td style=\"text-align: left\">Residual Std. Error</td><td>62365.07 (df=888)</td><td>59334.91 (df=888)</td><td>58416.53 (df=888)</td><td>56048.94 (df=888)</td><td>55075.80 (df=888)</td><td>51619.50 (df=888)</td></tr><tr><td style=\"text-align: left\">F Statistic</td><td>3.40<sup>**</sup> (df=3; 888)</td><td>3.43<sup>**</sup> (df=3; 888)</td><td>3.44<sup>**</sup> (df=3; 888)</td><td>3.38<sup>**</sup> (df=3; 888)</td><td>3.39<sup>**</sup> (df=3; 888)</td><td>3.24<sup>**</sup> (df=3; 888)</td></tr>\n",
       "<tr><td colspan=\"7\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align: left\">Note:</td><td colspan=\"6\" style=\"text-align: right\"><sup>*</sup>p&lt;0.1; <sup>**</sup>p&lt;0.05; <sup>***</sup>p&lt;0.01</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "##### Objective:\n",
    "\n",
    "# This script runs placebo Difference-in-Differences regressions on \"Employment\" (Yit_Emprego), \n",
    "# using fake post-treatment years to test for spurious effects.\n",
    "\n",
    "##### Workflow:\n",
    "\n",
    "#####Loops through a set of worksheet-year pairs, reading data for each period.\n",
    "\n",
    "    # Filters for the appropriate years (pre-/post-fake treatment).\n",
    "    # Constructs treatment and \"fake post-treatment\" indicators.\n",
    "    # Runs OLS regressions for each sample with robust standard errors.\n",
    "    # Aggregates results in a Stargazer-formatted table for easy reporting and export.\n",
    "\n",
    "!pip install stargazer\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.formula.api as smf\n",
    "from stargazer.stargazer import Stargazer\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "# 1) File path and worksheet-to-false-post-treatment-year mapping\n",
    "file_path = 'Data Placebo Test1.xlsx'\n",
    "sheets_and_post_years = {\n",
    "    '2014_2015': 2015,\n",
    "    '2014_2016': 2016,\n",
    "    '2014_2017': 2017,\n",
    "    '2015_2016': 2016,\n",
    "    '2015_2017': 2017,\n",
    "    '2016_2017': 2017\n",
    "}\n",
    "\n",
    "# 2) Lists to store regression results and associated labels\n",
    "placebo_models = []\n",
    "placebo_labels  = []\n",
    "\n",
    "# 3) Iterate over each worksheet: run placebo DID regressions for 'Employment'\n",
    "for sheet_name, placebo_post_year in sheets_and_post_years.items():\n",
    "    # Read data from the worksheet\n",
    "    df = pd.read_excel(file_path, sheet_name=sheet_name)\n",
    "    df.rename(columns={'Yit_Emprego': 'Employment'}, inplace=True)\n",
    "    \n",
    "    # Filter data to only contain the two relevant years (pre and placebo post)\n",
    "    placebo_pre_year = int(sheet_name.split('_')[0])\n",
    "    df = df[df['Ano'].isin([placebo_pre_year, placebo_post_year])].copy()\n",
    "    \n",
    "    # Create treatment and placebo post-period indicators\n",
    "    df['Treat'] = np.where(df['Situacao'] == 'Reonerado', 1, 0)\n",
    "    df['FalsePost'] = np.where(df['Ano'] == placebo_post_year, 1, 0)\n",
    "    \n",
    "    # Estimate placebo DID model with robust standard errors (HC1)\n",
    "    model = smf.ols('Employment ~ Treat * FalsePost', data=df).fit(cov_type='HC1')\n",
    "    \n",
    "    placebo_models.append(model)\n",
    "    placebo_labels.append(f'{placebo_pre_year} & {placebo_post_year}')\n",
    "\n",
    "# 4) Generate summary table in Stargazer format\n",
    "stargazer = Stargazer(placebo_models)\n",
    "stargazer.title(\"Placebo Tests for the First Difference-in-Differences Model\")\n",
    "stargazer.custom_columns(placebo_labels, [1] * len(placebo_models))\n",
    "stargazer.significant_digits(2)\n",
    "\n",
    "stargazer.rename_covariates({\n",
    "    'Treat': 'Treat',\n",
    "    'FalsePost': 'FalsePost',\n",
    "    'Treat:FalsePost': 'Treat×FalsePost'\n",
    "})\n",
    "stargazer.covariate_order([\n",
    "    'Intercept',    # or 'const' if needed by statsmodels output\n",
    "    'Treat',\n",
    "    'FalsePost',\n",
    "    'Treat:FalsePost'\n",
    "])\n",
    "\n",
    "# 5) Display results in the Jupyter notebook\n",
    "display(HTML(stargazer.render_html()))\n",
    "\n",
    "# 6) Save results to HTML file\n",
    "with open('placebo_did_emprego.html', 'w', encoding='utf-8') as f:\n",
    "    f.write(stargazer.render_html())\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9b2b74c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
